package com.itheima.controller;


import com.itheima.domain.vo.*;
import com.itheima.exception.BusinessException;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private  UserService service;

    @GetMapping("/{currentPage}/{pageSize}/{username}")
    public Result getAll(@PathVariable Integer currentPage,
                         @PathVariable Integer pageSize,
                         @PathVariable String username
                         ){
        //组装查询数据
        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setPageSize(pageSize);
        queryPageBean.setCurrentPage(currentPage);
        HashMap<String, String> queryParam = new HashMap<>();
        queryParam.put("username",username);
        queryPageBean.setQueryParams(queryParam);
        //执行查询
        return handlerQuery(queryPageBean);
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public Result getAll(@PathVariable Integer currentPage,
                         @PathVariable Integer pageSize
    ){
        if(currentPage < 0){
            throw new BusinessException(Code.BUSINESS_ERR,"当前页码不能小于0");
        }
        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setPageSize(pageSize);
        queryPageBean.setCurrentPage(currentPage);
        HashMap<String, String> queryParam = new HashMap<>();
//        queryParam.put("username","");
        queryPageBean.setQueryParams(queryParam);
        //执行查询
        return handlerQuery(queryPageBean);
    }

    //restful的post表示新增
    @PostMapping
    public Result save(@RequestBody AddUser au){
        boolean result = service.addUser(au);
        Integer code = result? Code.SAVE_OK : Code.SAVE_ERR;
        String message = result? "保存成功" : "保存失败";
        return new Result(code,result,message);
    }

    @PutMapping
    public Result update(@RequestBody UpdateUser uu){
        service.updateUser(uu);
        return new Result(Code.UPDATE_OK,true,"修改成功!");
    }

    @DeleteMapping("/{uid}")
    public Result delete(@PathVariable Integer uid){
        service.deleteUser(uid);
        return new Result(Code.DELETE_OK,true,"删除成功!");
    }

//    @DeleteMapping("/{id}")
//    public Result delete(@PathVariable Integer id){
//        boolean result = service.delete(id);
//        Integer code = result? Code.DELETE_OK : Code.DELETE_ERR;
//        String message = result? "删除成功" : "删除失败";
//        return new Result(code,result,message);
//    }

    private Result handlerQuery(QueryPageBean queryPageBean){
        PageResult page = service.findByPage(queryPageBean);
        Integer code = page.getRows() != null && page.getRows().size() != 0? Code.GET_OK : Code.GET_ERR;
        String message = page.getRows() != null && page.getRows().size() != 0? "查询全部成功" : "查询全部失败";
        return new Result(code,page,message);
    }


}
